<template>
	<view>
		<view class="body">
			<view class="address">
				<block v-for="(item,index) in adresslist" :key="index">
					<view class="item">
						<view class="img">
							<image src="../../../static/icon/address_location.png" mode=""></image>
						</view>
						<view class="content" style="width: 73%;">
							<view class="information">
								<text class="name">{{ item.name }}</text>
								<text class="phone">{{ item.phone }}</text>
								<template v-if="item.isdefault == 1">
									<uni-tag :inverted="true" text="默认" type="error" size="mini" />
								</template>
							</view>
							<view class="c-address">
								<view class="">
									{{item.Area.province}}{{item.Area.city}}{{item.Area.region}} {{item.detail}}
								</view>
							</view>
						</view>
						<view class="icon">
							<uni-icons type="compose" size="20" color="#666666"
								@click.stop="jumpClick(item)"></uni-icons>
							<uni-icons type="trash" size="20" color="#666666"
								@click.stop="delClick(item,index)"></uni-icons>
						</view>
					</view>
				</block>
			</view>

			<!-- 新建地址 -->
			<view class="add-address">
				<view class="add-btn" @click="addClick">
					新增地址
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				adresslist: [],
			}
		},
		onLoad() {

		},
		onShow() {
			this.getList()
		},
		methods: {
			getList() {
				let _this = this
				_this.$api.get('mall.address/lists', {}).then(res => {
					if (res.code == 1) {
						_this.adresslist = res.data.list
					}
				}, fail => {})
			},
			jumpClick(item) {
				uni.navigateTo({
					url: '/pages/user/address/edit?id=' + item.address_id
				})
			},
			delClick(item, index) {
				let _this = this
				if(item.isdefault == 1){
					_this.toast('无法删除默认地址')
					return false
				}
				uni.showModal({
					title: '提示',
					content: '确定删除吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								title: '提交中...',
								mask: true
							})
							_this.$api.post('mall.address/del', {
								id: item.address_id
							}).then(res => {
								uni.hideLoading()
								_this.toast(res.msg)
								if (res.code == 1) {
									let list = _this.adresslist
									list.splice(index, 1)
									_this.adresslist = list
								} else {
									_this.toast(res.msg)
								}
							}, fail => {})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			addClick() {
				uni.navigateTo({
					url: '/pages/user/address/add'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F6F6F6;
		padding-bottom: 200rpx;
	}

	.item {
		width: calc(100vw - 80rpx);
		padding: 32rpx 40rpx 24rpx 40rpx;
		background-color: #FFFFFF;
		margin-top: 26rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	/* 头像 */
	.item .img {
		width: 66rpx;
		height: 66rpx;
	}

	.item image {
		width: 66rpx;
		height: 66rpx;
		border-radius: 50%;
	}

	.item .icon {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	/* 昵称 */
	.name {
		font-size: 32rpx;
		font-weight: bold;
		margin-right: 4rpx;
		vertical-align: middle;
	}

	/* 号码 */
	.phone {
		font-size: 24rpx;
		color: #666666;
		margin-right: 8rpx;
		vertical-align: middle;
	}

	/* 默认标志 */
	.state {
		font-size: 20rpx;
		color: #FF2828;
		border: 1rpx solid #FF2828;
		border-radius: 4rpx;
		padding: 2rpx;
		vertical-align: middle;
	}

	/* 地址 */
	.c-address {
		margin-top: 10rpx;
		font-size: 26rpx;
	}

	/* 新增地址 */
	.add-address {
		width: 100%;
		position: fixed;
		bottom: 60rpx;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.add {
		display: inline-block;
		width: 38rpx;
		height: 38rpx;
		border-radius: 50%;
		background-color: var(--base-color);
		line-height: 35rpx;
		text-align: center;
		color: #FFFFFF;
		font-size: 30rpx;
		font-weight: bold;
		margin-right: 10rpx;
	}

	.add-btn {
		width: 80%;
		background: #000000;
		border-radius: 50rpx;
		font-weight: 800;
		font-size: 28rpx;
		color: #FFFFFF;
		padding: 20rpx 0;
		text-align: center;
	}
</style>